<?php

namespace common\models\base;

use Yii;

/**
 * This is the model class for table "{{%base_dist}}".
 *
 * @property int $id
 * @property string $name 名称
 * @property string $code 区号代码
 * @property string $abbr
 * @property int $level
 * @property int $parent_id
 */
class BaseDist extends \common\base\ActiveRecord {

    use \common\traits\QueryInstanceActiveRecord;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%base_dist}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['code'], 'required'],
            [['level', 'parent_id'], 'integer'],
            [['name'], 'string', 'max' => 50],
            [['code'], 'string', 'max' => 6],
            [['abbr'], 'string', 'max' => 20],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'name' => '名称',
            'code' => '区号代码',
            'abbr' => 'Abbr',
            'level' => 'Level',
            'parent_id' => 'Parent ID',
        ];
    }

    public static function getUnifinedName($code) {
        $code = array_unique($code);
        $data = static::find()
        ->where(['code' => $code])
        ->orderBy(['level' => SORT_ASC])
        ->select('name')
        ->column();
        return implode(' ', $data);
    }

}
